package ui.approval;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import o.财务单据类型;
import o.进货单据类型;
import o.销售单据类型;
import blservice.document.DocumentBLService;
import client.boot.BLServiceManager;
import tool.MyButton;
import tool.MyLabel;
import ui.commodity.StockOverflowDocumentPanel;

import ui.commodity.StockSendDocumentPanel;
import ui.finance.收付款单Panel;
import ui.finance.现金费用单Panel;
import ui.importandsaleman.ImportAndReturnDocumentPanel;
import ui.importandsaleman.SaleAndReturnDocumentPanel;
import vo.DocumentVo;
import vo.UserVo;
import vo.StockScrapAndOverflowDocumentVo;
import vo.StockSendingDocumentVo;
import vo.AccountInAndOutDocumentVo;
import vo.CashExpensesDocumentVo;
import vo.ImportAndReturnDocumentVo;
import vo.SaleAndReturnDocumentVo;

public class 单据审批panel extends JPanel{
	private static final long serialVersionUID = 1L;
	// private DocumentBLService djcxBL;
//	private DocumentBLService hcBL;
	private UserVo user;
	private JTable table;
	private JComboBox<String> cbDocumentType;
	private JButton detials;
	private JButton btnRedRushAndCopy;
	private JButton btnRedRush;
	private List<DocumentVo> list;

	public 单据审批panel(UserVo user) {
		this.user=user;
		this.setOpaque(false);
		setLayout(null);
		setSize(646, 433);
		

		JLabel lblDocumentType = new MyLabel("\u5355\u636E\u7C7B\u578B");
		lblDocumentType.setForeground(Color.WHITE);
		lblDocumentType.setBounds(50, 10, 70, 15);
		add(lblDocumentType);

		cbDocumentType = new JComboBox<String>();
		cbDocumentType.setModel(new DefaultComboBoxModel<String>(new String[] { "销退售单",
				 "进退货单", "收付款单",  "库存报损报溢单",  "库存赠送单", "现金费用单" }));

		cbDocumentType.setBounds(113, 7, 78, 21);
		cbDocumentType.addActionListener(e->filter());
		add(cbDocumentType);

		table = new JTable(){		
			private static final long serialVersionUID = 8999208543419910360L;

			@Override
			public boolean isCellEditable(int row, int column) {
				if(column==0){
					return true;
				}else{
					return false;
				}
			};
		};
		
		JScrollPane jsp=new JScrollPane(table);
		jsp.setBounds(47, 35, 578, 353);
		add(jsp);
		
		btnRedRush = new MyButton("Graphics/Button115-23.png");
		btnRedRush.setText("修改并审批");
		btnRedRush.addActionListener(e->editAndApprove());
		btnRedRush.setBounds(286, 398, 115, 23);
		add(btnRedRush);
		
		
		btnRedRushAndCopy = new MyButton("Graphics/Button115-23.png");
		btnRedRushAndCopy.setText("批量通过审批");
		btnRedRushAndCopy.addActionListener(e->batchApprove());
		btnRedRushAndCopy.setBounds(480, 398, 115, 23);
		add(btnRedRushAndCopy);
		
		detials = new MyButton("Graphics/Button125-23.png");
		detials.setText("查看并审批");
		detials.addActionListener(e->showAndApprove());
		detials.setBounds(86, 398, 125, 23);
		add(detials);

		filter();
	}
	
	private void filter(){
		try{
		DocumentBLService documentBL=getDocumentBLByType();
		list=documentBL.getUnapproval();
		}catch(RemoteException | MalformedURLException | SQLException
				| NotBoundException e) {
			JOptionPane.showMessageDialog(this,"无法连接到服务器"+e.getMessage());
			e.printStackTrace();
		}		
		display();
	}
	
	private DocumentBLService getDocumentBLByType() throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		switch((String)cbDocumentType.getSelectedItem()){
		case "销退售单":
			return BLServiceManager.load销退售单BLService(null);	
		case "进退货单":
			return BLServiceManager.load进退货单BLService(null);
		case "收付款单":
			return BLServiceManager.load收付款单BLService(null);
		case "现金费用单":
			return BLServiceManager.load现金费用单BLService(null);
		case "库存报损报溢单":
			return BLServiceManager.load库存报溢报损单BLService(null);
		
		case "库存赠送单":
			return BLServiceManager.load库存赠送单BLService(null);
		default:
			return null;
		}
		
	}
	
	private void batchApprove(List<Integer> selectedRow){
		try{
			DocumentBLService documentBL=getDocumentBLByType();
			if(documentBL==null){
				JOptionPane.showMessageDialog(this,"请填写正确的单据类型");
			}
		for(int i:selectedRow){
			try{
				documentBL.approve(list.get(i));
			}catch(ClassCastException e){
				JOptionPane.showMessageDialog(this, "请选择正确的单据类型");
				e.printStackTrace();
			}
			
		}
		}catch(RemoteException | MalformedURLException | SQLException
				| NotBoundException e) {
			JOptionPane.showMessageDialog(this,"无法连接到服务器"+e.getMessage());
			e.printStackTrace();
		}		
		filter();
	}
	
	private void display(){
		DefaultTableModel model = new DefaultTableModel();
		model.setColumnIdentifiers(new String[] {"","单据编号", "业务员","客户","仓库"});
		
		for (int i=0;i<list.size();i++) {
			model.addRow(new Object[] {false,list.get(i).get单据编号(),list.get(i).get业务员(),list.get(i).get客户(),list.get(i).get仓库()});
			
		}
		table.setModel(model);
		table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JCheckBox()));
		table.getColumnModel().getColumn(0).setCellRenderer(new MyTableCellRenderer());
		table.getColumnModel().getColumn(0).setMaxWidth(25);
	}
	private void showAndApprove(){
		if(table.getSelectedRow()==-1){
			JOptionPane.showMessageDialog(this, "请选中你想审批的单据\n" );
		}else{
			showDocumentDialog(false,true,false);
		}	
		
	}
	private void editAndApprove(){
		if(table.getSelectedRow()==-1){
			JOptionPane.showMessageDialog(this, "请选中你想审批的单据\n" );
		}else{
			showDocumentDialog(true,true,false);
		}
		
	}
	private void batchApprove(){
		ArrayList<Integer> selectedRow=new ArrayList<Integer>();
		for(int i=0;i<table.getRowCount();i++){
			if((boolean)table.getValueAt(i, 0)){
				selectedRow.add(i);
			}
		}
		batchApprove(selectedRow);
	
		
	}
	
	private void showDocumentDialog(boolean editable,boolean 审批,boolean 红冲){
		JDialog dialog=new JDialog(){
			private static final long serialVersionUID = -1313482603992533277L;

			@Override
			public void paint(Graphics g) {
				this.setBackground(new Color(0, 0, 0, 0));
				Image background = Toolkit.getDefaultToolkit().getImage(
						"Graphics/framebackground.jpg");
				g.drawImage(background, 0, 0, null);
				super.paint(g);
			}
		};
		dialog.getContentPane().setLayout(null);
		dialog.setBounds(200,200,750,480);
		JPanel panel=null;
		try{
		switch((String)cbDocumentType.getSelectedItem()){
		case "销退售单":
			panel=new SaleAndReturnDocumentPanel(user, (SaleAndReturnDocumentVo)list.get(table.getSelectedRow()),editable,销售单据类型.销售单,审批,红冲);
			break;
		
		case "进退货单":
			panel=new ImportAndReturnDocumentPanel(user, (ImportAndReturnDocumentVo)list.get(table.getSelectedRow()),editable,进货单据类型.进货单,审批,红冲);
			break;
		
		case "收付款单":
			panel=new 收付款单Panel(user, (AccountInAndOutDocumentVo)list.get(table.getSelectedRow()),editable,财务单据类型.收款单,审批,红冲);
			break;
		
		case "现金费用单":
			panel=new 现金费用单Panel(user, (CashExpensesDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		case "库存报损报溢单":
			panel=new StockOverflowDocumentPanel(user, (StockScrapAndOverflowDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		
		case "库存赠送单":
			panel=new StockSendDocumentPanel(user, (StockSendingDocumentVo)list.get(table.getSelectedRow()),editable,审批,红冲);
			break;
		default:
			return;
		}
		}catch(ClassCastException e){
			JOptionPane.showMessageDialog(this, "请选择正确的单据类型");
			e.printStackTrace();
		}
		panel.setBounds(0, 0, 746, 433);
		dialog.getContentPane().add(panel);
		dialog.setVisible(true);
	}
	
	class MyTableCellRenderer extends JCheckBox implements TableCellRenderer{

		private static final long serialVersionUID = 2479283536746389307L;

		@Override
		  public Component getTableCellRendererComponent(JTable table,
		    Object value, boolean isSelected, boolean hasFocus, int row,
		    int column) {
		    Boolean b = (Boolean) value;    
		             this.setSelected(b.booleanValue());    
		             return this;    

		  }
		  
		 }
}
